<?php

!defined('IN_TIPASK') && exit('Access Denied');

class attachcontrol extends base {

    function attachcontrol(& $get,& $post) {
        $this->base( & $get,& $post);
        $this->load('attach');
    }

    function onupload() {
	//HTML5上传
	if(isset($_SERVER['HTTP_CONTENT_DISPOSITION'])){
		if(preg_match('/attachment;\s+name="(.+?)";\s+filename="(.+?)"/i',$_SERVER['HTTP_CONTENT_DISPOSITION'],$info)){
			$temp_name=ini_get("upload_tmp_dir").'\\'.date("YmdHis").mt_rand(1000,9999).'.tmp';
			file_put_contents($temp_name,file_get_contents("php://input"));
			$size=filesize($temp_name);
			$_FILES['filedata']=array('name'=>$info[2],'tmp_name'=>$temp_name,'size'=>$size,'type'=>'','error'=>0);
		}
	}

	$upfile=$_FILES['filedata'];
	if(!isset($upfile)) exit(tjson_encode(array('msg' => '', 'err' =>$_SERVER['HTTP_CONTENT_DISPOSITION'].'文件域的name错误!')));
	if(empty($upfile['tmp_name']) || $upfile['tmp_name'] == 'none') exit(tjson_encode(array('msg' => '', 'err' =>'无文件上传!')));

    $ext=extname($upfile['name']);
    $targetfile= 'data/attach/'.gmdate('ym', $this->time).'/'.random(8).'.'.$ext;
    $status=$_ENV['attach']->movetmpfile($upfile,$targetfile);
    if(0==$status) {
        exit(tjson_encode(array('msg' => '', 'err' =>'文件上传失败，请联系网站管理员!')));
    }

    $_ENV['attach']->add($upfile['name'],$ext,$fsize,$targetfile);
    
    exit(tjson_encode(array('msg' =>$targetfile,'err' => '')));

         
    }



}
?>